package com.knowif.common.exception;

import com.knowif.system.model.MessageEnum;
import com.knowif.system.model.RestBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class BusinessExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(BusinessExceptionHandler.class);

    @ExceptionHandler(value = BusinessException.class)
    public RestBean<?> handleServiceException(BusinessException be){
        logger.error("handleServiceException - {}", be.getMsg());
        logger.error(be.getMessage());
        be.printStackTrace();
        return new RestBean<>(be.getCode(), be.getMsg());
    }

    @ExceptionHandler
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public RestBean<?> handleException(Exception e) {
        logger.error(e.getMessage());
        e.printStackTrace();
        return new RestBean<>(MessageEnum.UNKNOW_ERROR, null);
    }
}
